﻿@{
    Layout = ViewBag.Layout;
}
@Scripts.Render("~/bundles/rsa")
@using CNKI.TPI.Web.Base;
<script type="text/javascript">
    function Login() {
        if ($.trim($('#usercode').val()) == "") {
            messageAlert("请输入用户名");
            return;
        }
        ;
        if (typeof ($('#imgcode')) != null && $('#imgcode').val() == "") {
            messageAlert("请输入验证码");
            return;
        }
        var searchtext = $("#usercode").val();
        if (reginputtext(searchtext)) {
            alert("用户名中有非法字符，请检查！");
            return false;
        }
        var password = $("#password").val();
        if (reginputtext(password)) {
            alert("密码中有非法字符，请检查！");
            return false;
        }
        
        setMaxDigits(129);
        $.ajax({
            type: "GET",
            url: timestamp("@Html.Raw(Url.Action("GetRSAKey", "Account"))"),
            async: false,
            success: function (msg) {
                var keys = msg.split('|');
                var key = new RSAKeyPair(keys[0], "", keys[1]);
                var password = $("#password").attr("value");
                var pwdRtn = password;
                if (password != null && password.length != 0 && "True" == "@ConfigHelper.IsEncryptPasswordEnabled()")
                {
                    pwdRtn = encryptedString(key, password);
                }
                $("#password").attr("value", pwdRtn);
                $("#LoginKey").attr("value", keys[2]);
                var loginForm = $('#LoginForm');
                loginForm.submit();
            },
            error: function (e) {

            }
        });
    }

    $(document).keydown(function (event) {
        if (event.keyCode == 13) {
            Login();
        }
    });
</script>
@model CNKI.TPI.Web.UI.Models.LoginModel
<div class="wrap power-login">
    <h2 class="power-login-t"><span>用户登录</span></h2>
    @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { id = "LoginForm", @class = "form form-h power-login-b" }))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
        bool anonymousControlFlag = CNKI.TPI.Web.Base.ConfigHelper.IsAnonymousControlEnabled();
        string anonymousUsername = CNKI.TPI.Web.Base.ConfigHelper.GetAnonymousAccount();
        string currentUsername = "";
        if (CNKI.TPI.Web.Base.SessionHelper.IsUserInfoExsit())
        {
            currentUsername = CNKI.TPI.Web.Base.SessionHelper.GetUserInfo().UserCode;
        }
        if (CNKI.TPI.Web.Base.SessionHelper.IsUserInfoExsit() && ((anonymousControlFlag && !anonymousUsername.Equals(currentUsername)) || !anonymousControlFlag))
        {
            CNKI.TPI.Web.Search.Model.LoginUserInfo userInfo = CNKI.TPI.Web.Base.SessionHelper.GetUserInfo();
            string username = string.IsNullOrEmpty(userInfo.UserName) ? userInfo.UserCode : userInfo.UserName;
            <div class="welcome">@username 您好！您没有访问该页面的权限</div>
            <div class="login-btn">
                @using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" }))
                {
                    @Html.AntiForgeryToken()
                    <a class="btn btn-m btn-submit btn-submit-l btn-warn-f" href="javascript:document.getElementById('logoutForm').submit()">注销</a>
                }
            </div>
        }
        else
        {
            <div class="f-gp">
                <div class="f-lb">
                    <label class="f-label">用户名：</label>
                </div>
                <div class="f-ct">
                    @Html.TextBoxFor(m => m.UserName, new { @class = "f-text span3", maxlength = "50", id = "usercode" })
                </div>
            </div>
            <div class="f-gp">
                <div class="f-lb">
                    <label class="f-label">密码：</label>
                </div>
                <div class="f-ct">
                    @Html.PasswordFor(m => m.Password, new { @class = "f-text span3", maxlength = "300", id = "password" })
                </div>
            </div>
            if (ViewBag.FaildNum >= 2)
            {
                <div class="f-gp">
                    <div class="f-lb">

                        <label class="f-label">验证码：</label>
                    </div>
                    <div class="f-ct">
                        @Html.TextBoxFor(m => m.VerificationCode, new { @class = "f-text", maxlength = "50", id = "imgcode" })
                        <img id="codeImg" class='yzm' alt="刷新验证码！" src="~/verify_code.ashx" onclick="this.src=this.src+'?'" />
                        <a class='yzm-a changecode' href="javascript:void()" onclick="javascript:$('#codeImg').trigger('click')">看不清？</a>
                    </div>
                </div>
            }
            <input id="LoginKey" name="LoginKey" value="" type="hidden">
            <div class="f-gp">
                <div class="f-lb">
                </div>
                <div class="f-ct">
                    <a href="javascript:Login();" class="btn btn-l btn-submit btn-warn-f">登录</a>
                </div>
            </div>
        }
    }
</div>
